package m.jclecai.com.view.klpk;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;


import java.util.List;

import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.utils.BaseUtils;

/**
 * Created by andy on 2017/6/6.
 * <p>
 * 这里我们创建一个Poker对象；
 * poker有多个种类的，但是其业务逻辑是一样的；
 */

public class PokerContainer extends FrameLayout implements View.OnClickListener {

    //扑克的样式；
    public static final int TYPE_PAIRS = 0;
    public static final int TYPE_SAME_COLOR = 1;
    public static final int TYPE_FLUSH = 2;
    public static final int TYPE_STRAIGHT_FLUSH = 3;
    public static final int TYPE_THREE_SHEETS = 4;

    public Status mCurStatus = Status.unchecked;
    private int mSpacing = BaseUtils.dip2px(getContext(), 17);

    //这里的话，我们通过type来区分每种扑克；

    public PokerContainer(Context context) {
        this(context, null);
    }


    public PokerContainer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PokerContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnClickListener(this);
    }

    /**
     * 获得当前的间距
     * @return
     */
    public int getSpacing() {
        return mSpacing;
    }

    /**
     * 通过这个方法我们是可以设置间距的；
     * @param spacing
     */
    public void setSpacing(int spacing) {
        mSpacing = BaseUtils.dip2px(getContext(),spacing);
    }

    public interface OnCheckedChangeListener {
        void isChecked(View view, boolean isChecked);
    }


    public OnCheckedChangeListener mOnCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public void onClick(View v) {
        changeStatus();
    }

    private void changeStatus() {
        //设置状态；
        if (mCurStatus.equals(Status.checked)) {
            mCurStatus = Status.unchecked;
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.isChecked(null, false);
            }
        } else {
            mCurStatus = Status.checked;
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.isChecked(null, true);
            }
        }
    }

    /**
     * 这个方法也是会走OnCheckedChangeListener里面的回掉方法的；
     *
     * @param isChecked
     */
    public void setChecked(boolean isChecked) {
        if (isChecked) {
            //这里表示我们需要将其设置为选中的状态；
            mCurStatus = Status.checked;
        } else {
            mCurStatus = Status.unchecked;
        }
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.isChecked(null, isChecked);
        }
    }

    public Status getCurStatus() {
        return mCurStatus;
    }

    public enum Status {
        checked, unchecked
    }

    public boolean isChecked() {
        return mCurStatus == Status.checked;
    }

    //// TODO: 2017/6/7 修改一下测量的规则；对宽度进一步测试；这里的话，主要是对宽度进行一个测量；
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMeasure = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
        int heightMeasure = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom() - getPaddingTop();

        int totalWidth = 0;
        int totalHeight = 0;

        int pokerWidth = 0;
        int pokerHeight = 0;
        //这里对Poker进行一个重新测量；
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthMeasure, MeasureSpec.AT_MOST);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasure, MeasureSpec.AT_MOST);
            //对其进行一个测量
            childAt.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            pokerWidth = pokerWidth > childAt.getMeasuredWidth() ? pokerWidth : childAt.getMeasuredWidth();

            pokerHeight = pokerHeight > childAt.getMeasuredHeight() ? pokerHeight : childAt.getMeasuredHeight();
        }
        totalWidth = pokerWidth + (getChildCount() - 1) * mSpacing;
        totalHeight = pokerHeight;
        setMeasuredDimension(
                MeasureSpec.makeMeasureSpec(totalWidth + getPaddingRight() + getPaddingLeft(), MeasureSpec.EXACTLY)
                ,
                MeasureSpec.makeMeasureSpec(totalHeight + getPaddingTop() + getPaddingBottom(), MeasureSpec.EXACTLY));
    }

    /**
     * 通过将List<PokerBean>来实现相应的布局；
     */
    public void setPokers(List<PokerBean> pokerBeen, int type) {
        //这里的话，我们是需要根据这个type来设置相应的布局；
        for (PokerBean pokerBean : pokerBeen) {
            addView(new Poker(getContext(),pokerBean,type).getView());
        }
    }

    /**
     * 对布局进行一个重新的编排；
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        //设置间距
        int margin_left = 0;
        //这里我们就可以简单的将其排列出来，但是这里明显是有问题的，
        // 因为我们这个Poker的view的测量是按照Framelayout，但是我们是不能按照这个测量规则来的；
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (i == 0) {
                child.layout(0, 0, childWidth, childHeight);
            } else {
                //将View向右平移Spacing单位的dp
                margin_left += mSpacing;
                child.layout(margin_left, 0, childWidth + margin_left, childHeight);
            }
        }
    }
}
